﻿#ifndef BIZEDITOR_H
#define BIZEDITOR_H

#include "common/baseeditor.h"
#include "m_fhs.h"
#include "support/sp_env.h"

class BizEditor : public BaseEditor
{
public:
    explicit BizEditor(QWidget *parent = 0):
        BaseEditor(parent)
    {
        loadColumnWidthFlag=false;
        part=m_getPart();
        userLand=Location::document + APP_ROOT;
    }

    QString m_getPart()
    {
        QSettings reg(APP_REG,QSettings::NativeFormat);
        return reg.value("part").toString();
    }

    void keyPressEvent(QKeyEvent *event)
    {
        if(event->key() == Qt::Key_Escape)
            close();
    }

    void closeEvent(QCloseEvent *)
    {
        saveColumnWidth();
    }

    void updateMenu()
    {
        menu->clear();

        if(isEdit)
        {
            menu->addAction(QStringLiteral("已完成"),this,&BizEditor::en_finish);
            menu->addSeparator();
            menu->addAction(QStringLiteral("撤销"),this,&BizEditor::revert);
            menu->addAction(QStringLiteral("保存"),this,&BizEditor::save);
            menu->addSeparator();
            menu->addAction(QStringLiteral("删除"),this,&BizEditor::en_remove);
            menu->addSeparator();
            menu->addAction(QStringLiteral("退出编辑模式"),this,&BizEditor::edit_off);
        }
        else
            generateMenu();
    }
    void updateView()
    {
        indicator = 1;
        model->setTable(table);
        model->setFilter(QString("date = '%1'").arg(Env::today));
        model->setSort(0,Qt::DescendingOrder);
        model->select();

        if(model->rowCount() < 1)
            updateViewMonth();

        setHeaderData();
    }

    void updateViewUnfinish()
    {
        indicator = 4;
        model->setTable(table);
        model->setFilter(QString("status != '%1'").arg(ST_FINISH));
        model->setSort(0,Qt::DescendingOrder);
        model->select();

        if(model->rowCount() < 1)
            updateViewAll();

        setHeaderData();
    }

    void updateViewMonth()
    {
        indicator = 2;
        QString preDate=Env::month + "-01";
        QString postDate=Env::month + "-31";

        model->setTable(table);
        model->setFilter(QString("date >= '%1' and date <= '%2'").arg(preDate).arg(postDate));
        model->setSort(0,Qt::DescendingOrder);
        model->select();

        if(model->rowCount() < 1)
            updateViewAll();

        setHeaderData();
    }
    void updateViewAll()
    {
        indicator = 3;
        model->setTable(table);
        model->setSort(0,Qt::DescendingOrder);
        model->select();

        setHeaderData();
    }

    virtual bool loadColumnWidth(){}
    virtual void saveColumnWidth(){}
    virtual void setHeaderData(){}
    virtual void en_finish(){}
    virtual void en_remove(){}

    virtual void removeItem(const QModelIndex &index)
    {
        if(model->data(index).toString() != ST_WAIT)
        {
            QMessageBox::warning(this,QStringLiteral("状态无效"),QStringLiteral("只有【等待处理】才可以删除！"));
            return;
        }
        model->removeRow(index.row());
    }

    virtual void finishItemDirect(const QString &table, int id)
    {
        QString status = getStatus(table,id);
        if(status == ST_FINISH)
            return;
        changeStatus(table,ST_FINISH,id);

        switch (indicator) {
        case 1:
            updateView();
            break;
        case 2:
            updateViewMonth();
            break;
        case 3:
            updateViewAll();
            break;
        case 4:
            updateViewUnfinish();
            break;
        }
    }
    virtual void cancelItemDirect(const QString &table,int id)
    {
        if(getStatus(table,id) != ST_WAIT)
        {
            QMessageBox::warning(this,QStringLiteral("状态无效"),QStringLiteral("只有【等待处理】才可以取消！"));
            return;
        }

        /*
        QString host=getValue(table,"host",id);
        if(!host.isEmpty() && host != Env::hostName )
        {
            QMessageBox::warning(this,QStringLiteral("无权操作"),QStringLiteral("您无权操作他人的记录！"));
            return;
        }
        */

        changeStatus(table,ST_CANCEL,id);

        switch (indicator) {
        case 1:
            updateView();
            break;
        case 2:
            updateViewMonth();
            break;
        case 3:
            updateViewAll();
            break;
        case 4:
            updateViewUnfinish();
            break;
        }
    }

    virtual void search()
    {
        showSearchBox("name");
    }

    int getId(const QString &table, const QString &uid) const
    {
        QSqlQuery query;
        query.exec(QString("select id from %1 where uid='%2'")
                   .arg(table)
                   .arg(uid));
        query.next();
        return query.value(0).toInt();
    }

    void changeStatus(const QString &table, const QString &status, int id)
    {
        QSqlQuery query;
        query.exec(QString("update %1 set status = '%2' where id = '%3'")
                   .arg(table)
                   .arg(status)
                   .arg(id));
    }

    QString getStatus(const QString &table, int id) const
    {
        QSqlQuery query;
        query.exec(QString("select status from %1 where id='%2'")
                   .arg(table)
                   .arg(id));
        query.next();
        return query.value(0).toString();
    }

    void sendMsg(const QString &title, const QString &content)
    {
        QByteArray data;
        QDataStream out(&data,QIODevice::WriteOnly);
        out << title << content ;

        QUdpSocket sender;
        sender.writeDatagram(data,QHostAddress("192.168.1.255"),SEND_PORT);

    }

protected:
    int indicator;
    QString part;
    QString userLand;

    bool loadColumnWidthFlag;
    QMap<int,int> colWidth;
};

#endif // BIZEDITOR_H
